﻿using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
namespace Iworld.ViewModel
{
    /// <summary>
    /// 缓存数据的管理者对象
    /// </summary>
    public class DataManager<IDataKey> where IDataKey : struct
    {
        #region 私有静态字段

        static Dictionary<IDataKey, object> pond = new Dictionary<IDataKey, object>();

        #endregion

        #region 静态方法

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="persistence">一个布尔值，标识缓存是否需要持久化</param>
        public static void SetValue(IDataKey key, object value, bool persistence = false)
        {
            if (persistence)
            {
                IsolatedStorageSettings.ApplicationSettings[key.ToString()] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
            else
            {
                if (pond.Any(c => c.Key.Equals(key)))
                {
                    pond[key] = value;
                }
                else
                {
                    pond.Add(key, value);
                }
            }
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="key">键</param>
        /// <param name="persistence">一个布尔值，标识所要操作的缓存是否持久化对象</param>
        /// <returns>返回缓存的值</returns>
        public static T GetValue<T>(IDataKey key, bool persistence = false)
        {
            if (persistence)
            {
                return (T)IsolatedStorageSettings.ApplicationSettings[key.ToString()];
            }
            else
            {
                if (pond.Any(c => c.Key.Equals(key)))
                {
                    return (T)pond[key];
                }
                else
                {
                    return default(T);
                }
            }
        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="persistence">一个布尔值，标识所要操作的缓存是否持久化对象</param>
        public static void RemoveValue(IDataKey key, bool persistence = false)
        {
            if (persistence)
            {
                IsolatedStorageSettings.ApplicationSettings.Remove(key.ToString());
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
            else
            {
                if (pond.Any(x => x.Key.Equals(key)))
                {
                    pond.Remove(key);
                }
            }
        }

        /// <summary>
        /// 判断缓存是否存在
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="key">键</param>
        /// <param name="persistence">一个布尔值，标识所要操作的缓存是否持久化对象</param>
        /// <returns>返回一个布尔值，表示缓存是否存在</returns>
        public static bool HaveValue<T>(IDataKey key, bool persistence = false)
        {
            if (persistence)
            {
                return IsolatedStorageSettings.ApplicationSettings.Any(x => x.Key == key.ToString() && x.Value is T);
            }
            else
            {
                return pond.Any(c => c.Key.Equals(key) && c.Value is T);
            }
        }

        #endregion
    }
}
